package minimips.isa.itype;

import minimips.computer.Computer;
import minimips.exception.StopInstructionException;
import minimips.util.BitUnits;

public class LdInstruction extends MemoryAccessingITypeInstruction {

	public LdInstruction(long memoryAddress, int rs, int rt,
			int offset) {
		super(memoryAddress, 55, "LD", rs, rt, offset);
	}
	
	@Override
	protected void fetchInstruction(Computer computer)
			throws StopInstructionException {
		super.fetchInstruction(computer);
		computer.addRegisterBeingUsed(this.getRt());
	}

	@Override
	protected void operateOnMemory(Computer computer) {
		long memoryValue = computer.getManyFromMemoryAddress((int)computer.getEx_mem_aluOutput().getRegisterValue(), BitUnits.DOUBLE_WORD);
		computer.getMem_wb_lmd().setRegisterValue(memoryValue);		
	}

	@Override
	protected void operateWriteBack(Computer computer) {
		int registerIndex = this.getOpcode() >> 16 & 31;
		computer.setProgrammableRegisterValue(registerIndex, computer.getMem_wb_lmd());
		computer.removeRegisterBeingUsed(this.getRt());
	}

	

}
